/*
 * ParaphraseDetectionCorpus.cpp
 *
 *  Created on: Feb 6, 2015
 *      Author: phong
 */

#include "PDCorpus.h"
#include "../Treebank.h"
#include "../Utils.h"
#include "../Depstruct.h"
#include "../Deptree.h"

using namespace std;

PDCorpus::PDCorpus(string filename, Dictionary* vocaDic, Dictionary* posDic, Dictionary* deprelDic, bool mirror) {
	cout << "load " << filename << endl;
	fstream reader(filename, ios::in);
	DEP* rows = new DEP[1000];
	string line;
	int length = 0;
	int count = 0;

	Deptree* tree1;
	Deptree* tree2;
	real score;

	while (getline(reader, line)) {
		vector<string> comps = Utils::splitStringWoRegex(line, "\t");

		if (comps.size() == 1) {
			count++;

			Depstruct* ds = Depstruct::create(rows, length, vocaDic, posDic, deprelDic);
			Deptree* tree = ds->toDepTree();
			if (count % 3 == 2)
				tree1 = tree;
			else if (count % 3 == 0) {
				tree2 = tree;
				storage.push_back(PDENTRY(score, tree1, tree2));
				if (mirror)
					storage.push_back(PDENTRY(score, tree2->clone(), tree1->clone()));
				Depstruct::free(ds);

				if (storage.size() % 100 == 0) {
					cout << storage.size() << "\r" << flush;
				}
			}

			length = 0;
		}
		else if (comps.size() == 2) {
			score = std::stoi(comps[1]) == 1 ? 1 : -0.3;
		}
		else {
			length++;
			rows[length-1] = DEP(stoi(comps[0]), comps[1], comps[4], stoi(comps[6]), comps[7]);
		}
	}

	delete[] rows;
}

PDCorpus::~PDCorpus() {
	for(tuple<real,Deptree*,Deptree*> tp : storage) {
		Deptree::free(std::get<1>(tp));
		Deptree::free(std::get<2>(tp));
	}
}

